package com.example.project.dao.impl;

import com.example.project.dao.IUserDao;
import com.example.project.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 *作者：金照富
 * 2023年11月3日
 */
@Repository
public class IUserDaoImpl implements IUserDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public int insertUser1(User user1) {
        return jdbcTemplate.update(
                "insert into user1(user1Id,user1Name,userPasswoed,Email,Poweruser) " +
                        "values(?,?,?,?,?,?)"
                ,user1.getUserId()
                ,user1.getUserName()
                ,user1.getUserPasswoed()
                ,user1.getEmail()
                ,user1.getPoweruser()
        );
    }

    @Override
    public int deleteUser1(User user1) {
        return jdbcTemplate.update(
                "delete from user1 " +
                "where User1ID=?",user1.getUserId());

    }

    @Override
    public int updateUser1(User user1) {
        return jdbcTemplate.update("update user1 set " +
                "User1ID=?,User1Name=?,userPasswoed=?,Email=?,Poweruser=?"
                ,user1.getUserId()
                ,user1.getUserName()
                ,user1.getUserPasswoed()
                ,user1.getEmail()
                ,user1.getPoweruser()
        );
    }

    @Override
    public List<User> getAllUser1(User user1) {
        if(user1.getUserName()!=null)
            return jdbcTemplate.query("select " +
                "user1Id,user1Name,userPasswoed,Email,Poweruser " +
                "from user1 where User1Name='"+user1.getUserName()+"'",new Object[]{},
                new BeanPropertyRowMapper<>(User.class));
        else
            return jdbcTemplate.query("select " +
                "user1Id,user1Name,userPasswoed,Email,Poweruser" +
                "from user1",new Object[]{},
                new BeanPropertyRowMapper<>(User.class));
    }

    @Override
    public User getUser1(User user1) {
        List<User> result=jdbcTemplate.query("select " +
                                "user1Id,user1Name,userPasswoed,Email,Poweruser " +
                                "from user1 where User1Name='"+user1.getUserName()+"'",
                        new Object[]{},
                        new BeanPropertyRowMapper<>(User.class));
        if (result.size()>=1)
            return result.get(0);
        else return null;
    }
}
